home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / DBPCXL15.ARJ / SRC_C.ARJ / PCX4.C < prev    next >
Text File  |  1992-01-26  |  4KB  |  113 lines

  1. #include <stdio.h>
  2. #include <io.h>
  3. #include <conio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <alloc.h>
  7. #include <mem.h>
  8. #include <dos.h>
  9. #include <errno.h>
  10.  
  11. #include "vidlib.h"
  12. #include "pcxlib.h"
  13.  
  14. /*/////////////////////////////////////////////////////////////////////////
  15. // fread4PCX
  16. //          expects destination to be organized as 1 byte per pixel
  17. ///////////////////////////////////////////////////////////////////////////*/
  18. int   fread4PCX(char *d, unsigned dw, unsigned dl, unsigned dx, unsigned dy,
  19.                 unsigned x0, unsigned y0, unsigned x1, unsigned y1, PCXF *p)
  20. {
  21.     int h,m,k;
  22.     char r,g,b,i,nibbles, tpal[3];
  23.     char *Plane0, *Plane1, *Plane2, *Plane3, *dest;
  24.  
  25.     if (pcxdebug) {
  26.        fprintf(pcxdebug,
  27.                "fread4PCX(%Fp, %u, %u,  %u, %u,  %u, %u,  %u, %u,  %Fp\n",
  28.                d, dw, dl, dx, dy, x0, y0, x1, y1, p);
  29.     }
  30.     /* Allocate memory for the scan line buffers */
  31.     Plane0=(char *)malloc(p->h->bytesline*4);
  32.     if (Plane0 == NULL) {
  33.        PCXerror=ENOMEM; return(-1);
  34.     }
  35.     Plane1=Plane0+p->h->bytesline;
  36.     Plane2=Plane1+p->h->bytesline;
  37.     Plane3=Plane2+p->h->bytesline;
  38.  
  39.     dest=(char *)normalize(d + dy*dw + dx);
  40.     /* 16 color PCX files interleve scanlines for each color    */
  41.     for (h=y0; h<y1+1; h++)  {
  42.         _read_pcx_line(p,Plane0, x0, x1);
  43.         _read_pcx_line(p,Plane1, x0, x1);
  44.         _read_pcx_line(p,Plane2, x0, x1);
  45.         _read_pcx_line(p,Plane3, x0, x1);
  46.         /* 16 color bitmaps have 4 bits per pixel               */
  47.         for ( m=0; m < p->h->bytesline; ++m) {
  48.             r = Plane0[m];
  49.             g = Plane1[m];
  50.             b = Plane2[m];
  51.             i = Plane3[m];
  52.             /* Combine a bit from each 4 scan lines into a 4-bit nibble */
  53.             nibbles = 0;
  54.             for ( k=0; k<4; ++k) {
  55.                 nibbles = 0;
  56.                 /* If the most significant bit is set... */
  57.                 /* Set the appropriate bit in the higher order nibble */
  58.                 if (r & '\x80') nibbles |= 0x10;
  59.                 if (g & '\x80') nibbles |= 0x20;
  60.                 if (b & '\x80') nibbles |= 0x40;
  61.                 if (i & '\x80') nibbles |= 0x80;
  62.                 r<<=1; g<<=1; b<<=1; i<<=1;
  63.                 /* Repeat for the lower order nibble */
  64.                 if (r & '\x80') nibbles |= 0x01;
  65.                 if (g & '\x80') nibbles |= 0x02;
  66.                 if (b & '\x80') nibbles |= 0x04;
  67.                 if (i & '\x80') nibbles |= 0x08;
  68.                 r<<=1; g<<=1; b<<=1; i<<=1;
  69.                 *dest++ = nibbles;
  70.             }
  71.         }
  72.         dest=(char *)normalize(d + (h-y0+dy)*dw + dx);
  73.     }
  74.     free(Plane0);
  75.     return(h-y0);
  76. }
  77.  
  78. /*/////////////////////////////////////////////////////////////////////////
  79. // fdisp4PCX
  80. ///////////////////////////////////////////////////////////////////////////*/
  81. int   fdisp4PCX(unsigned dx, unsigned dy,
  82.                 unsigned x0, unsigned y0, unsigned x1, unsigned y1, PCXF *p)
  83. {
  84.     int h,m,k,wmode,wplanes;
  85.     char r,g,b,i,nibbles, tpal[3];
  86.     char *dest;
  87.  
  88.     wmode=What_Wmode(); wplanes=What_WPlanes;
  89.     Select_Wmode(0);
  90.     if (pcxdebug) {
  91.        fprintf(pcxdebug,
  92.                "fdisp4PCX(%u, %u,  %u, %u,  %u, %u,  %Fp\n",
  93.                dx, dy, x0, y0, x1, y1, p);
  94.     }
  95.  
  96.     dest=(char *)normalize(_screen_start + dy*_screen_width + dx);
  97.     /* 16 color PCX files interleve scanlines for each color    */
  98.     for (h=y0; h<y1+1; h++)  {
  99.         Select_WPlane(1);
  100.         _read_pcx_line(p,dest, x0, x1);
  101.         Select_WPlane(2);
  102.         _read_pcx_line(p,dest, x0, x1);
  103.         Select_WPlane(4);
  104.         _read_pcx_line(p,dest, x0, x1);
  105.         Select_WPlane(8);
  106.         _read_pcx_line(p,dest, x0, x1);
  107.         dest=(char *)normalize(dest + _screen_width);
  108.     }
  109.     Select_Wmode(wmode); Select_WPlane(wplanes);
  110.     return(h);
  111. }
  112.  
  113.